package misc;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {
	
	public static void main(String[] args) {
		
		if (args.length == 5) {
			sendMail(new StringBuffer(args[0]), new StringBuffer(args[1]), args[3], args[4], args[2], args[5], args[6]);
		}
	}
	
	public static void sendMail (StringBuffer subject, StringBuffer content, String sender, String recipient, String smtpserver, String username, String password) {
		
		Properties props = new Properties();
		props.put("mail.transport.protocol", "smtp");
		props.put("mail.smtp.host", smtpserver);
		props.put("mail.smtp.auth", true);
		
		Authenticator auth = new SMTPAuthenticator();
		Session session = Session.getDefaultInstance(props, auth);
		MimeMessage message = new MimeMessage(session);
		Transport transport;

		try {
			transport = session.getTransport();
			message.setFrom(new InternetAddress(sender));
			InternetAddress a[] = new InternetAddress[1];
			a[0] = new InternetAddress(recipient);
			
			message.setRecipients(Message.RecipientType.TO, a);
			message.setSubject(subject.toString());
			message.setContent(content.toString(), "text/plain");
			
			transport.connect();
			transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
			transport.close();
		
		} catch (AddressException e) {
			e.printStackTrace();
		
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

}
